package com.aizhixin.lab.project.template.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.course.template.entity.TemplateChapter;
import com.aizhixin.lab.course.template.entity.TemplateChapterFile;
import com.aizhixin.lab.course.template.repository.TemplateChapterRepository;
import com.aizhixin.lab.course.template.service.TemplateChapterService;
import com.aizhixin.lab.project.course.domain.TeacherKnowledgePointFileDomain;
import com.aizhixin.lab.project.course.entity.TeacherKnowledgePoint;
import com.aizhixin.lab.project.course.entity.TeacherKnowledgePointFile;
import com.aizhixin.lab.project.course.repository.TeacherKnowledgePointFileRepository;
import com.aizhixin.lab.project.template.domain.KnowledgePointDomain;
import com.aizhixin.lab.project.template.entity.KnowledgePoint;
import com.aizhixin.lab.project.template.entity.KnowledgePointFile;
import com.aizhixin.lab.project.template.repository.KnowledgePointFileRepository;
import com.aizhixin.lab.project.template.repository.KnowledgePointRepository;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Slf4j
@Service
@Transactional
public class KnowledgePointService {
    @Autowired
    private KnowledgePointRepository knowledgePointRepository;
    @Autowired
    private KnowledgePointFileRepository fileRepository;
    @Autowired
    private TemplateChapterService templateChapterService;
    @Autowired
    private TemplateChapterRepository chapterRepository;
    /**
     * 查询任务下所有知识点
     *
     * @param moduleTaskId
     * @return
     */
    public List<KnowledgePointDomain> findKnowledgePointDomain(String moduleTaskId) {
        List<KnowledgePoint> knowledgePointList = knowledgePointRepository.findByModuleTaskId(moduleTaskId);
        List<KnowledgePointDomain> domainList = new ArrayList<>();
        for (KnowledgePoint knowledgePoint : knowledgePointList) {
            KnowledgePointDomain knowledgePointDomain = new KnowledgePointDomain();
            BeanUtils.copyProperties(knowledgePoint, knowledgePointDomain);
            if (knowledgePoint.getType()!=null) {
                if (10 == knowledgePoint.getType()) {
                    if (knowledgePoint.getMinutiaId() != null) {
                        List<TemplateChapterFile> fileList = templateChapterService.getFileList(knowledgePoint.getMinutiaId());
                        knowledgePointDomain.setTemplateChapterFileList(fileList);
                    } else if (knowledgePoint.getChapterId() != null) {
                        List<TemplateChapterFile> fileList = templateChapterService.getFileList(knowledgePoint.getChapterId());
                        knowledgePointDomain.setTemplateChapterFileList(fileList);
                    }
                }
            }
            List<KnowledgePointFile> fileList = fileRepository.findByKnowledgePointIdAndDeleteFlag(knowledgePoint.getId(), DataValidity.VALID.getState());
            List<TeacherKnowledgePointFileDomain> fileDomainList = new ArrayList<>();
            fileList.forEach(item->{
                TeacherKnowledgePointFileDomain teacherKnowledgePointFileDomain = new TeacherKnowledgePointFileDomain();
                BeanUtils.copyProperties(item,teacherKnowledgePointFileDomain);
                fileDomainList.add(teacherKnowledgePointFileDomain);
            });
            knowledgePointDomain.setKnowledgePointFileList(fileDomainList);
            domainList.add(knowledgePointDomain);
        }
        return domainList;
    }
    public Map<String,Object> findKnowledgePointInfo(String id){
        Map<String,Object> result=new HashMap<>();
        KnowledgePoint one = knowledgePointRepository.findOne(id);
        KnowledgePointDomain knowledgePointDomain = new KnowledgePointDomain();
        BeanUtils.copyProperties(one,knowledgePointDomain);
        List<KnowledgePointFile> fileList = fileRepository.findByKnowledgePointIdAndDeleteFlag(id, DataValidity.VALID.getState());
        List<TeacherKnowledgePointFileDomain> fileDomainList = new ArrayList<>();
        fileList.forEach(item->{
            TeacherKnowledgePointFileDomain teacherKnowledgePointFileDomain = new TeacherKnowledgePointFileDomain();
            BeanUtils.copyProperties(item,teacherKnowledgePointFileDomain);
            fileDomainList.add(teacherKnowledgePointFileDomain);
        });
        knowledgePointDomain.setKnowledgePointFileList(fileDomainList);
        result.put(ApiReturnConstants.SUCCESS,true);
        result.put(ApiReturnConstants.DATA,knowledgePointDomain);
        return result;
    }
    /**
     * 修改或编辑知识点
     * @param domain
     * @return
     */
    public String save(KnowledgePointDomain domain) {
        KnowledgePoint knowledgePoint;
        if (domain.getType()==20) {
            Boolean isNew=false;
            if (StringUtils.isEmpty(domain.getId())) {
                knowledgePoint = new KnowledgePoint();
                domain.setId(UUID.randomUUID().toString());
                BeanUtils.copyProperties(domain, knowledgePoint);
            } else {
                knowledgePoint = knowledgePointRepository.findOne(domain.getId());
                BeanUtils.copyProperties(domain, knowledgePoint);
            }
            List<KnowledgePointFile> fileList=new ArrayList<>();
            domain.getKnowledgePointFileList().forEach(item->{
                KnowledgePointFile teacherKnowledgePointFile = new KnowledgePointFile();
                BeanUtils.copyProperties(item,teacherKnowledgePointFile);
                if (StringUtils.isEmpty(teacherKnowledgePointFile.getId())){
                    teacherKnowledgePointFile.setKnowledgePointId(domain.getId());
                    teacherKnowledgePointFile.setId(UUID.randomUUID().toString());
                }
                fileList.add(teacherKnowledgePointFile);
            });
            if (isNew){
                fileRepository.deleteByKnowledgePointId(domain.getId());
            }
            fileRepository.save(fileList);
            knowledgePointRepository.save(knowledgePoint);
        }else {
            TemplateChapter chapter = chapterRepository.findByIdAndDeleteFlag(domain.getMinutiaId(), DataValidity.VALID.getState());
            if (!Objects.isNull(chapter)) {
                domain.setKnowledgePoint(chapter.getName());
            }
            if (StringUtils.isEmpty(domain.getId())) {
                knowledgePoint = new KnowledgePoint();
                domain.setId(UUID.randomUUID().toString());
                BeanUtils.copyProperties(domain, knowledgePoint);
            } else {
                knowledgePoint = knowledgePointRepository.findOne(domain.getId());
                BeanUtils.copyProperties(domain, knowledgePoint);
            }

            knowledgePointRepository.save(knowledgePoint);
        }
        return knowledgePoint.getId();
    }

    /**
     * 删除知识点
     * @param id
     * @return
     */
    public Map<String,Object> delete(String id) {
        Map<String,Object> result=new HashMap<>();
        KnowledgePoint one = knowledgePointRepository.findOne(id);
        if (Objects.isNull(one)){
            result.put(ApiReturnConstants.SUCCESS,false);
            result.put(ApiReturnConstants.ERROR,"没有知识点信息");
            return result;
        }
        knowledgePointRepository.delete(id);
        result.put(ApiReturnConstants.SUCCESS,true);
        result.put(ApiReturnConstants.DATA,"删除成功");
        return result;
    }
}
